package com.wei.controller;


import com.wei.model.HistoryUrlsDO;
import com.wei.service.IHistoryUrlsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/history")
@RequiredArgsConstructor
public class HistoryUrlsController {

    private final IHistoryUrlsService historyUrlsService;

    @PostMapping("/record")
    public String recordHistoryUrl(@RequestBody Map<String, Object> requestData) {
        String url = (String) requestData.get("url");

        System.out.println("🎯 接收到前端记录请求: " + url);

        HistoryUrlsDO historyUrlsDO = new HistoryUrlsDO();
        historyUrlsDO.setUrl(url);
        historyUrlsDO.setVisitTime(new Date()); // 后端自己设置时间

        boolean saved = historyUrlsService.save(historyUrlsDO);
        System.out.println("💾 保存结果: " + saved);
        System.out.println("📝 保存的记录ID: " + historyUrlsDO.getId());

        return "success";
    }
}
